
window.serverLocation = "http://spitfire-project.eu:8182/ld4s";


var aggregateJSON = {
	"step1" : {
		"smart-service-proxy" : {
			"type" : "input",
			"label" : "proxy location:"
		},
		"query-name" : {
			"type" : "input",
			"label" : "query name:"
		},
		"dependsOnSensorValues" : {
			"type" : "requeset",
			"value" : "true"
		},
		"multiNodeQuery" : {
			"type" : "requeset",
			"value" : "true"
		}

	},
	"step2" : {
		"platform" : {
			"key" : "onPlatform",
			"type" : "sortable",
			"dataType" : "ssn",
			"label" : "platform"
		},
		"uom" : {
			"key" : "uom",
			"type" : "sortable",
			"dataType" : "spt",
			"label" : "Unit of Measurement"
		},
		"type-device" : {
			"key" : "a",
			"type" : "sortable",
			"dataType" : "rdfs",
			"label" : "device type"
		},
		"obs" : {
			"key" : "obs",
			"type" : "sortable",
			"dataType" : "spt",
			"label" : "Observed Property"
		},
		"foi" : {
			"key" : "foi",
			"type" : "sortable",
			"dataType" : "spt",
			"label" : "Feature of Interest"
		},
		"hasLocation" : {
			"key" : "hasLocation",
			"type" : "sortable",
			"dataType" : "dul",
			"label" : "location name"
		},
		"point" : {
			"key" : "point",
			"type" : "sortable",
			"dataType" : "spt",
			"label" : "location coords"
		},
		"status" : {
			"key" : "status",
			"type" : "sortable",
			"dataType" : "spt",
			"label" : "platform Status"
		},
		"implement" : {
			"key" : "implement",
			"type" : "sortable",
			"dataType" : "ssn",
			"label" : "Algorithms in use"
		},
		"deployment" : {
			"key" : "hasDeployment",
			"type" : "sortable",
			"dataType" : "ssn",
			"label" : "deployment"
		}
	},
	"step3" : {

	},
	"step4" : {

	}
};

// here store the data of the prefix
var SendPREFIX = {
	"dul" : "PREFIX dul:     <http://www.w3.org/2002/07/owl#>\n",
	"ssn" : "PREFIX ssn:      <http://www.w3.org/2005/xpath-functions#>\n",
	"spt" : "PREFIX spt:     <http://jena.hpl.hp.com/ARQ/property#>\n",
	"rdfs" : ""// no prefix
};

// this function for sending the query
function Sendql(url, data) {
	console.log("sending query:" + JSON.stringify(data));
	console.log("sending post url:" + JSON.stringify(url));

	$.ajax({
		url : url,
		type : "post"
	});

	// get the result

	// here should be the code which get the result

	
}

// after send all data and it should have a function to get the result
function showResult() {
	//some getting function or post function the data
	
	var returnData="some getting function or post function the data\nsome getting function or post function the data";
	$("#result").html("");
	$("#result").codeArea(returnData);
}

// main
$(function() {

	createAggregateHtml($("#aggregateDiv"), aggregateJSON.step1);
	createAggregateHtml($("#UnAdd"), aggregateJSON.step2);
	$(".item").draggable({
		appendTo : "body",
		helper : "clone"
	});

	$("#rightContent").droppable(
			{
				drop : function(event, ui) {
					ui.draggable.appendTo($("#Add"));
					if ($("#" + $(ui.draggable).attr("key")).length == 0) {
						var html = "<div  key='" + $(ui.draggable).attr("key")
								+ "'><table><tr><td><span class='label'>" + $(ui.draggable).html() + ":</span></td>"
								+ "<td><input disabled datatype='" + $(ui.draggable).attr("datatype")
								+ "' type='text' id='" + $(ui.draggable).attr("key") + "' /></td>"
								+ "<td><input type='checkbox' key='" + $(ui.draggable).attr("key") + "' id='"
								+ $(ui.draggable).attr("key") + "check'/><label for='" + $(ui.draggable).attr("key")
								+ "check'>set as const</label></td></tr></div>";
						$("#constDiv").append(html);
						$("#" + $(ui.draggable).attr("key") + "check").button().click(
								function() {
									var key = $(this).attr("key");
									if ($("#" + key).attr("disabled") == "disabled"
											|| $("#" + key).attr("disabled") == "true") {
										$("#" + key).val("");
										$("#" + key).attr("disabled", false);
									} else {
										$("#" + key).val("");
										$("#" + key).attr("disabled", true);
									}
									$("#" + key).attr("disabled");

								});
						$("#note").hide();

					}

				}
			});

	$("#leftContent").droppable({
		drop : function(event, ui) {
			ui.draggable.appendTo($("#UnAdd"));
			$("div[key=" + $(ui.draggable).attr("key") + "]").remove();
			if ($("#Add").find(".item").length == 0) {
				$("#note").show();
			}
		}
	});

	$("#showResult").button().click(
			function() {
				// get step 1 data
				var smartServiceProxy = $("#smart-service-proxy").val();
				var queryName = $("#query-name").val();
				if (queryName.length == 0 || smartServiceProxy.length == 0) {
					alert("query name or smart service proxy is empty");
					return false;
				}
				//
				var dependsOnSensorValues = "yes";
				var multiNodeQuery = "yes";
				// create the encoding of the query

				var criterias = $("#rightContent").find("li");
				if (criterias.length <= 0) {
					alert("please drag the criterias to the box");
					return false;
				}

				var constSqarqlPREFIX = "";
				var constSqarqlContent = "";
				$.each($("#constDiv").find("input[type=text]"), function(key) {
					if ($(this).attr("datatype") != "" && $(this).attr("datatype") != undefined)
						var encodedSparql = "";
					// when it is not a const
					if ($(this).attr("disabled") == "disabled" || $(this).attr("disabled") == "true") {
						encodedSparql += SendPREFIX[$(this).attr("datatype")];
						if ($(this).attr("datatype") != "rdfs") {
							encodedSparql += "SELECT ?element ?entity WHERE {\n\t" + "?element "
									+ $(this).attr("datatype") + ":" + $(this).attr("id") + " ?entity .\n}";
						} else {
							encodedSparql += "SELECT ?element ?entity WHERE {\n\t ?element a ?entity .\n}";
						}
						//
						var href = "http://" + smartServiceProxy + "/?elementsQuery=" + encodeURI(encodedSparql)
								+ "&name=" + queryName + "&dependsOnSensorValues=" + dependsOnSensorValues
								+ "&multiNodeQuery=" + multiNodeQuery;

						// do ajax function
						Sendql(href, encodedSparql);

					} else {
						if (constSqarqlPREFIX.indexOf(SendPREFIX[$(this).attr("datatype")]) == -1)
							constSqarqlPREFIX += SendPREFIX[$(this).attr("datatype")];
						if ($(this).attr("datatype") == "rdfs") {
							constSqarqlContent += "\t ?element a \"" + $(this).val() + "\".\n";
						} else {
							constSqarqlContent += "\t ?element " + $(this).attr("datatype") + ":" + $(this).attr("id")
									+ " \"" + $(this).val() + "\" .\n";

						}

					}

				});// end of each

				// send the const ql
				if (constSqarqlContent != "" || constSqarqlContent != null || constSqarqlContent.length != 0) {
					var constsparql = constSqarqlPREFIX + "\nSELECT ?element WHERE {\n" + constSqarqlContent + "}";
					var href = "http://" + smartServiceProxy + "/?elementsQuery=" + encodeURI(constsparql) + "&name="
							+ queryName + "&dependsOnSensorValues=" + dependsOnSensorValues + "&multiNodeQuery="
							+ multiNodeQuery;

					Sendql(href, constsparql);
				}
				showResult();
			});
	

});

// function to create html
function createAggregateHtml(jqparent, json) {

	$.each(json, function(key) {
		switch (this.type) {
		case "input":
			var _default = {
				"label" : "",
				"labelStyle" : "label",
				"style" : "",
				"type" : "input",
				"defalutValue" : "",

			};
			var $v = $.extend(_default, this);

			var html = "<div class='" + $v.style + "'><table><tr>" + "<td><span class='" + $v.labelStyle + "'>"
					+ $v.label + "</span></td>" + "<td>" + "<input type='text' id='" + key + "'/>" + "</td>"
					+ "</tr></table></div>";
			jqparent.append(html);
			initialInput();
			break;
		case "sortable":
			var _default = {
				"key" : "",
				"type" : "sortable",
				"dataType" : "",
				"label" : ""
			};
			var $v = $.extend(_default, this);
			var html = "<li class=\"ui-state-default item\" key='" + $v.key + "' dataType='" + $v.dataType + "'>"
					+ $v.label + "</li>";
			jqparent.append(html);
			break;
		}// end of each

	});// end of each
}